<template>
	<view>
		<z-paging
			:show-loading-more-no-more-view="false"
			ref="paging"
			:auto="false"
			:hide-empty-view="keyword.trim().length <= 0"
			:refresher-enabled="false"
			v-model="dataList"
			@query="queryList"
		>
			<view class="bgc" slot="top">
				<u-navbar
					:fixed="false"
					:title="$t('search.navText')"
					:autoBack="true"
					bgColor="transparent"
				></u-navbar>
				<view class="search">
					<u--input
						confirmType="search"
						@confirm="getInput"
						:placeholder="$t('home.search')"
						border="none"
						suffixIcon="search"
						suffixIconStyle="size:20px"
						v-model="keyword"
					></u--input>
				</view>
			</view>
			<view v-if="keyword.trim().length <= 0">
				<view class="record" v-if="historyList.length > 0">
					<view class="title">
						<u--text :text="$t('search.record')" color="#141414" bold size="18"></u--text>
						<u-icon name="trash" color="#bac1c8" size="20" @click="removeSearch"></u-icon>
					</view>
					<view class="search-list">
						<view
							class="search-item"
							@click="setInput(item)"
							v-for="(item, index) in historyList"
							:key="index"
						>
							{{ item }}
						</view>
					</view>
				</view>
			</view>
			<view class="content" v-if="keyword.trim().length > 0">
				<storeItem
					:item="item"
					v-for="(item, index) in dataList"
					:key="index"
					@goDetail="goDetail"
				></storeItem>
			</view>
		</z-paging>
		<!-- <language></language> -->
	</view>
</template>

<script>
import { mapGetters } from 'vuex';
export default {
	data() {
		return {
			dataList: [],
			keyword: '',
			historyList: []
		};
	},
	onShow() {
		this.searcHistory();
	},
	computed: {
		...mapGetters(['isLogin'])
	},
	methods: {
		goDetail(item) {
			this.isLogin
				? uni.$u.route('/pagesA/home/merchantDetail?id=' + item.id)
				: uni.$u.route('/pagesA/my/login');
		},
		queryList(pageNo, pageSize) {
			this.$http
				.post('index/searchShop', {
					page: pageNo,
					limit: pageSize,
					keywords: this.keyword,
					user_location: uni.getStorageSync('user_location')
				})
				.then(res => {
					if (res.code == 1) {
						res.code == 1
							? this.$refs.paging.complete(res.data.shop.data)
							: this.$refs.paging.complete(false);
					}
				});
		},
		removeSearch() {
			uni.showModal({
				title: '提示',
				content: '确定删除搜索记录吗?',
				success: res => {
					if (res.confirm) {
						uni.removeStorageSync('history');
						this.historyList = [];
					} else if (res.cancel) {
					}
				}
			});
		},
		setInput(text) {
			this.keyword = text;
			this.$refs.paging.reload();
		},
		getInput(text) {
			if (text.trim().length <= 0) {
				this.toast('搜索内容能为空');
			} else {
				this.$refs.paging.reload();
				let history = uni.getStorageSync('history');
				if (history) {
					if (history.length >= 10) {
						history.shift(); //先删除
						history.push(text); //后添
						uni.setStorageSync('history', history);
					} else {
						history.push(text);
						uni.setStorageSync('history', history);
					}
				} else {
					let list = [];
					list.push(text);
					uni.setStorageSync('history', list);
				}
			}
		},
		searcHistory() {
			let history = uni.getStorageSync('history');
			if (history) {
				this.historyList = history;
			} else {
				this.historyList = [];
			}
		}
	}
};
</script>
<style>
page {
	background-color: #f4f4f5;
}
</style>
<style lang="scss" scoped>
/deep/.u-icon__icon {
	font-size: 28px !important;
}

.bgc {
	background-image: url();
	background-repeat: no-repeat;
	background-size: 100% 100%;
	height: 250rpx;
	height: 250rpx;
	position: relative;

	.search {
		position: absolute;
		left: 50%;
		bottom: -20px;
		width: 90%;
		transform: translateX(-50%);
		background-color: #fff;
		border-radius: 88rpx;
		box-shadow: 0px 9px 19px 0px rgba(218, 218, 218, 0.5);
		padding: 25rpx 40rpx;
		z-index: 10080 !important;
		box-sizing: border-box;
	}
}
.content {
	padding: 0 30rpx;
	margin-top: 80rpx;
}
.record {
	padding: 0 30rpx;
	margin-top: 80rpx;

	.title {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 20rpx;
	}

	.search-list {
		display: flex;
		align-items: center;
		flex-wrap: wrap;

		.search-item {
			background-color: #fff;
			padding: 11rpx 25rpx;
			border-radius: 30rpx;
			color: #757785;
			font-size: 28rpx;
			margin: 0 20rpx 20rpx 0;
		}
	}
}
</style>
